﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using ExpertMIS.Codes;

namespace ExpertMIS.UserControl
{
    public partial class ucColumnList : System.Web.UI.UserControl
    {
        private void GetColumnArticle(string ColumnOID, DataList dlArticleList)
        {
            ADMArticleBroker broker = new ADMArticleBroker();

            dlArticleList.DataSource = broker.GetArticlesByColumn(ColumnOID, ArticleCountOnColumn);
            dlArticleList.DataBind();
        }

        private void RefreshColumns()
        {
            ADMColumnBroker broker = new ADMColumnBroker();
            IList<ADMColumnEntity> list = broker.GetRootColumns(ColumnCount);

            dlColumns.DataSource = list;
            dlColumns.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshColumns();
        }

        protected void dlColumns_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataList dlRecommendArticle = e.Item.FindControl("dlRecommendArticle") as DataList;
                ADMColumnEntity entity = e.Item.DataItem as ADMColumnEntity;
                if (dlRecommendArticle != null && entity != null)
                {
                    GetColumnArticle(entity.OID, dlRecommendArticle);
                }

                HyperLink hlMore = e.Item.FindControl("hlMore") as HyperLink;
                if (hlMore != null && entity != null)
                {
                    hlMore.ToolTip = "参看更多关于[" + entity.Name + "]的内容";
                    hlMore.NavigateUrl = "";
                }
            }
        }

        protected string CutLongTitle(string text)
        {
            return Utility.CutLongText(text);
        }

        public int ArticleCountOnColumn
        {
            get;
            set;
        }

        public int ColumnCount
        {
            get;
            set;
        }

        protected void dlRecommendArticle_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ADMArticleEntity article = e.Item.DataItem as ADMArticleEntity;
                HyperLink hlArticle = e.Item.FindControl("hlArticle") as HyperLink;
                if (article != null && hlArticle != null)
                {
                    hlArticle.Text = CutLongTitle(article.Title);
                    hlArticle.NavigateUrl = "~/Columns/Article.aspx?oid=" + article.OID;
                    hlArticle.ToolTip = article.Title;
                }
            }
        }
    }
}